<template>
  <div class="app-container">
    <el-button type="primary" icon="el-icon-plus" size="mini" style="margin-bottom: 10px;" @click="centerDialogVisible = true">手动添加主机</el-button>
    <el-button type="primary" icon="el-icon-place" size="mini" style="margin-bottom: 10px;">Ansible捕获主机</el-button>
    <el-table v-loading="listLoading" :data="list" row-key="id" border fit highlight-current-row style="width: 100%">
      <!-- <el-table-column align="center" label="ID" width="45">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column> -->

      <el-table-column width="140px" align="center" label="主机名">
        <template slot-scope="{row}">
          <span>{{ row.hostname }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="50px" label="IP">
        <template slot-scope="{row}">
          <span>{{ row.ip }}</span>
        </template>
      </el-table-column>

      <el-table-column width="60px" align="center" label="User">
        <template slot-scope="{row}">
          <span>{{ row.ansible_user }}</span>
        </template>
      </el-table-column>

      <el-table-column width="100px" label="私钥">
        <template slot-scope="{row}">
          <span>{{ row.ansilbe_key }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="项目组" width="110">
        <template slot-scope="{row}">
          <el-tag
            v-for="tag in row.groups"
            :key="tag.name">
            {{tag.nickName}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="系统" width="110">
        <template slot-scope="{row}">
          <span>{{ row.system }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="添加时间" width="110">
        <template slot-scope="{row}">
          <span>{{ row.create_time | timeSubstr }}</span>
        </template>
      </el-table-column>      
      <!-- <el-table-column class-name="status-col" label="备注" width="110">
        <template slot-scope="{row}">
          <span>{{ row.mark }}</span>
        </template>
      </el-table-column>       -->
      <el-table-column class-name="status-col" label="状态" width="60">
        <template slot-scope="{row}">
          <i class="el-icon-warning" :style="[{'font-size':'25px', 'color': row.status }]"></i>

        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="操作" >
        <template slot-scope="{row}">
          <!-- <el-button type="text" @click="drawer = true" >详细资料</el-button><br> -->
          <el-button type="text" @click="check_online(row)">在线检测</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="添加主机"
      :visible.sync="centerDialogVisible"
      width="60%"
      center>

        <el-row class="demo-autocomplete">
          <el-col :span="12">
                <div class="demo-input-suffix" style="padding-bottom: 5px">主机名</div>
                <el-input v-model="hostname" style="width:98%"></el-input><br>
              <div class="demo-input-suffix" style="padding-bottom: 5px">Ansible User</div>
                <el-input v-model="ansible_user" style="width:98%"></el-input>
          </el-col>
          <el-col :span="12">
              <div class="demo-input-suffix" style="padding-bottom: 5px;">IP 地址</div>
              <el-input v-model="ip" style="width:98%"></el-input>
              <div class="demo-input-suffix" style="padding-bottom: 5px">Ansible 私钥</div>
                <el-input v-model="ansible_key" style="width:98%"></el-input>
          </el-col>
        </el-row><br>

      <span slot="footer" class="dialog-footer">
        <el-button plain size="small" @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" plain size="small"  @click="submit_playbook">添 加</el-button>
      </span>
    </el-dialog>
    <el-drawer
      title="详细资料"
      :visible.sync="drawer"
      :direction="direction">
      <span>我来啦!</span>
    </el-drawer>

  </div>
</template>

<script>
import { fetchList } from '@/api/ansibleui'
import Sortable from 'sortablejs'
import YamlEditor from '@/components/yamlEditor'

export default {
  name: 'DragTable',
  components: { YamlEditor },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    timeSubstr(msg) {
      return msg.substr(0,10)
    }
  },
  data() {
    return {
      ip: "",
      hostname: "",
      ansible_user: "",
      ansible_key: "",
      centerDialogVisible: false,
      drawer: false,
      direction: 'rtl',
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      sortable: null,
      oldList: [],
      newList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    submit_playbook(){},
    check_online(row){
      console.log('修改颜色')
      row.status = 'red'
    },
    async getList() {
      this.listLoading = true
      const { data } = await fetchList('hosts', this.listQuery)
      this.list = data.items
      this.total = 200
      this.listLoading = false
      // this.oldList = this.list.map(v => v.id)
      // this.newList = this.oldList.slice()
      // this.$nextTick(() => {
      //   this.setSort()
      // })
    },
    // setSort() {
    //   const el = this.$refs.dragTable.$el.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]
    //   this.sortable = Sortable.create(el, {
    //     ghostClass: 'sortable-ghost', // Class name for the drop placeholder,
    //     setData: function(dataTransfer) {
    //       // to avoid Firefox bug
    //       // Detail see : https://github.com/RubaXa/Sortable/issues/1012
    //       dataTransfer.setData('Text', '')
    //     },
    //     onEnd: evt => {
    //       const targetRow = this.list.splice(evt.oldIndex, 1)[0]
    //       this.list.splice(evt.newIndex, 0, targetRow)

    //       // for show the changes, you can delete in you code
    //       const tempIndex = this.newList.splice(evt.oldIndex, 1)[0]
    //       this.newList.splice(evt.newIndex, 0, tempIndex)
    //     }
    //   })
    // }
  }
}
</script>

<style>
.sortable-ghost{
  opacity: .8;
  color: #fff!important;
  background: #42b983!important;
}
</style>

<style scoped>
.icon-star{
  margin-right:2px;
}
.drag-handler{
  width: 20px;
  height: 20px;
  cursor: pointer;
}
.show-d{
  margin-top: 15px;
}

</style>
